import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import store from "@/store/index";
import { close, start } from '@/utils/nprogress'

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue"),
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to: any, from: any, next: any) => {
  start();
  //未登陆
  if (!window.localStorage.getItem("token") && to.path !== "/login") {
      return next({ path: "/login" });
  };
  //已登陆
  if(window.localStorage.getItem("token")&&to.path == "/login") return next({ path: "/" });
  //重新加载动态路由
  if (!store.state.permissionList&&to.path!='/login') {
      // router.removeRoute('router');
      return store.dispatch("FETCH_PERMISSION").then(() => {
          next({ ...to, replace: true });
      });
  } else {
      next();
  }
});
router.afterEach((to: any, from: any, next: any) => {
  close();
  try {
      //设置标题
      if (to.meta.name) {
          document.title = to.meta.name;
      }
  } catch (err) {}
  let routerList = to.matched;
  //顶部面包屑
  store.commit("setCrumbList", routerList);
  //目前左边导航选中的active
  store.commit("SET_CURRENT_MENU", to.name);
});

/* 固定的路由 */
/*
    meta:{
        name:侧边栏名字,
        icon:侧边栏图标,
        hide:是否在侧边栏隐藏
    }
*/ 
export const DynamicRoutes = [
  {
    path: "",
    component:  () => import("@/components/nav/nav.vue"),
    name: "container",
    // redirect: 'home',
    meta: {
        // requiresAuth: true,
        name: "首页",
        icon: "HomeFilled",
    },
    children: [
      {
        "component":"",
        "path": "board",
        "name":"cproduct-manage",
        "meta":{
            "icon": "Postcard",
            "name": "数据看板",
        },
        "children": [
            {
              "component": () => import("@/views/board/workbench.vue"),
                "path": "/workbench",
                "name":"workbench",
                "meta":{
                    "icon": "Monitor",
                    "name": "工作台",
                },
            },
            {
              "component": () => import("@/views/board/transactionStatistics.vue"),
              "path": "/transactionStatistics",
              "name":"transactionStatistics",
              "meta":{
                  "icon": "Money",
                  "name": "交易统计",
              },
            },
            {
              "component": () => import("@/views/board/appointmentBoard.vue"),
              "path": "/appointmentBoard",
              "name":"appointmentBoard",
              "meta":{
                  "icon": "Calendar",
                  "name": "预约看板",
              },
            },
            {
              "component": () => import("@/views/board/pageData.vue"),
              "path": "/pageData",
              "name":"pageData",
              "meta":{
                  "icon": "Box",
                  "name": "页面数据",
              },
            }
        ],
      },
      
      {
        "component":"",
        "path": "consultingMane",
        "name":"consultingMane",
        "meta":{
            "icon": "DataAnalysis",
            "name": "咨询管理",
        },
        "children": [
            {
              "component": () => import("@/views/consultingMana/appointment.vue"),
                "path": "/appointment",
                "name":"appointment",
                "meta":{
                    "icon": "MessageBox",
                    "name": "预约记录",
                },
            },
            {
              "component": () => import("@/views/consultingMana/consultingDetail.vue"),
                "path": "/consultingDetail",
                "name":"consultingDetail",
                "meta":{
                    "name": "预约详情",
                    "hide": true
                },
            },
        ]
      },  
      {
        "component":"",
        "path": "orderMana",
        "name":"orderMana",
        "meta":{
            "icon": "SetUp",
            "name": "订单管理",
        },
        "children": [
            {
              "component": () => import("@/views/orderMana/consultingOrder.vue"),
                "path": "/consultingOrder",
                "name":"consultingOrder",
                "meta":{
                    "icon": "FolderOpened",
                    "name": "咨询订单",
                },
            },
            {
              "component": () => import("@/views/orderMana/orderDetails.vue"),
                "path": "/orderDetails",
                "name":"orderDetails",
                "meta":{
                    "icon": "FolderOpened",
                    "name": "订单详情",
                    "hide": true
                },
            },
            {
              "component": () => import("@/views/orderMana/changeCons.vue"),
                "path": "/changeCons",
                "name":"changeCons",
                "meta":{
                    "icon": "FolderOpened",
                    "name": "更换咨询师",
                    "hide": true,
                    "isSaved": false
                },
                
            },
        ]
      },
      {
        "component":"",
        "path": "dataMana",
        "name":"dataMana",
        "meta":{
            "icon": "DocumentCopy",
            "name": "资料管理",
        },
        "children": [
            {
              "component": () => import("@/views/dataMana/userMana.vue"),
                "path": "/userMana",
                "name":"userMana",
                "meta":{
                    "icon": "Edit",
                    "name": "用户管理",
                },
            },
            {
              "component": () => import("@/views/dataMana/addUser.vue"),
                "path": "/addUser",
                "name":"addUser",
                "meta":{
                    "name": "新增用户",
                    "hide": true
                },
            },
            {
              "component": () => import("@/views/dataMana/userInfo.vue"),
                "path": "/userInfo",
                "name":"userInfo",
                "meta":{
                    "name": "用户信息",
                    "hide": true
                },
            },
            {
              "component": () => import("@/views/dataMana/consultantMana.vue"),
                "path": "/consultantMana",
                "name":"consultantMana",
                "meta":{
                    "icon": "IceTea",
                    "name": "咨询师管理",
                },
            },
            {
              "component": () => import("@/views/dataMana/addConsultant.vue"),
                "path": "/addConsultant",
                "name":"addConsultant",
                "meta":{
                    "name": "新增咨询师",
                    "hide": true
                },
            },           
            {
              "component": () => import("@/views/dataMana/consultantServiceMana.vue"),
                "path": "/consultantServiceMana",
                "name":"consultantServiceMana",
                "meta":{
                    "icon": "Fries",
                    "name": "咨询服务管理",
                },
            },
            {
              "component": () => import("@/views/dataMana/addConsult.vue"),
                "path": "/addConsult",
                "name":"addConsult",
                "meta":{
                    "name": "新增咨询服务",
                    "hide": true
                },
            }, 
            {
              "component": () => import("@/views/dataMana/studioMana.vue"),
                "path": "/studioMana",
                "name":"studioMana",
                "meta":{
                    "icon": "School",
                    "name": "工作室管理",
                },
            },
            {
              "component": () => import("@/views/dataMana/userAppoint.vue"),
                "path": "/userAppoint",
                "name":"userAppoint",
                "meta":{
                    "icon": "School",
                    "name": "预约咨询师",
                    "hide": true
                },
            },
            {
              "component": () => import("@/views/dataMana/caseManage.vue"),
                "path": "/caseManage",
                "name":"caseManage",
                "meta":{
                    "icon": "School",
                    "name": "个案管理",
                    "hide": true
                },
            },
        ]
      },
      {
        "component":"",
        "path": "rosteringMana",
        "name":"rosteringMana",
        "meta":{
            "icon": "Calendar",
            "name": "排班管理",
        },
        "children": [
            {
              "component": () => import("@/views/rosteringMana/rosteringTime.vue"),
                "path": "/rosteringTime",
                "name":"rosteringTime",
                "meta":{
                    "icon": "Timer",
                    "name": "排班时间",
                },
            },
            {
              "component": () => import("@/views/rosteringMana/rosteringRule.vue"),
                "path": "/rosteringRule",
                "name":"rosteringRule",
                "meta":{
                    "icon": "Calendar",
                    "name": "设置排班规则",
                },
            },
        ]
      },
      {
        "component":"",
        "path": "agencyMana",
        "name":"agencyMana",
        "meta":{
            "icon": "OfficeBuilding",
            "name": "机构管理",
        },
        "children": [
            {
              "component": () => import("@/views/agencyMana/systemMana.vue"),
                "path": "/systemMana",
                "name":"systemMana",
                "meta":{
                    "icon": "Setting",
                    "name": "系统设置",
                },
            },
            {
              "component": () => import("@/views/agencyMana/operationLogs.vue"),
                "path": "/operationLogs",
                "name":"operationLogs",
                "meta":{
                    "icon": "Notebook",
                    "name": "操作记录",
                },
            },
        ]
      },
    ]
  },
  
]

export default router
